
var idIndex;
var id;
$("#table").bootstrapTable({
    url:"role/querys",
    method:'post',
    toolbar:'#toolbar',
    contentType:'application/x-www-form-urlencoded',
    sidePagination:'server',
    pageNumber:1,
    pageSize:5,
    // queryParams:function(params){
    //     params["username"] = $("#username").val();
    //     params["role.id"] = $("#roleCondition").val();
    //     // params["username"] = $("#username").val();
    //     // params["username"] = $("#username").val();
    //     // params["username"] = $("#username").val();
    //     // params["username"] = $("#username").val();
    //     console.log(params)
    //     return params;
    // },
    pageList:[5, 25, 50, 100,'ALL'],
    dataType:'json',
    cache:false,
    pagination:true,
    showRefresh:true,
    showColumns:true,
    detailView:true,

    detailFormatter:function (index,row) {

        var html="<div class='quanxianDdetail'><div style='overflow: hidden;float: left;margin-left: 50px'>"
        var count=0;
        $.ajax({
            url:"jueSe/quanxian",
            type:"post",
            contentType:'application/x-www-form-urlencoded',
            dataType:'json',
            async:false,
            success:function(data) {
                var size=data.rows
               // console.log(row)
                var k=0;

                //循环一级菜单显示
                for(var i=0;i< size.length;i++){

                    for(var t=0;t<row.jsqxList.length;t++){
                            if(row.jsqxList[t].qid==size[i].id){k=t}
                    }
                       if(row.jsqxList[i]!=null && row.jsqxList[k]!=null && row.jsqxList[k].qid==size[i].id && size[i].qxdengji==0){
                           k++;
                           html+=('<p class="colorRed" style="color: red" "><input type="checkbox"  onchange="waring(this)" checked  id="check'+index+i+'" ><label dataid="'+size[i].id+'" for="check'+index+i+'">'+size[i].qxName+'</label></p>')
                       }else{
                           html+=('<p class="colorRed" style="color: red" "><input type="checkbox"  onchange="waring(this)" id="check'+index+i+'" ><label dataid="'+size[i].id+'" for="check'+index+i+'">'+size[i].qxName+'</label></p>')
                       }
                    //循环一级菜单下的二级菜单

                         var erMenu=row.jsqxList[0].quanxian.erMenu
                           for(var b=0;b<erMenu.length;b++ ) {
                               if (erMenu[b].qxdengji == size[i].id ) {
                                   var bool=false;
                                   for(var x=0;x<row.jsqxList.length;x++){
                                   if(row.jsqxList[x].quanxian.parent!=null&&row.jsqxList[x].quanxian.id==erMenu[b].id){
                                       //当前角色有的权限
                                       html += ('<p  class="erMenu"><input type="checkbox"  onchange="erMenu(this)"checked  id="check' + index + b + i + '" ><label dataid="' + erMenu[b].id + '" for="check' + index + b + i + '">' + erMenu[b].qxName + '</label></p>')
                                        bool=true;
                                   }
                                   }
                                   if(bool==false){
                                       html += ('<p class="erMenu"><input type="checkbox"  onchange="erMenu(this)"  id="check' + index + b + i + '" ><label dataid="' + erMenu[b].id + '" for="check' + index + b + i + '">' + erMenu[b].qxName + '</label></p>')
                                   }


                               }
                           }
                        //如果与一级菜单ID相同则显示
                    html+="</div><div style='overflow: hidden;float: left;margin-left: 50px''>"
                    }

             }
            })
        html+='</div></div><div style="float: right;margin-right: 20px"><button class="btn btn-success"  onclick="baocun(this,'+row.id+')">保存</button></div>';
        return html;
    },
     classes:'table table-hover table-striped',
    columns:[
        {
            field:'dd',
            title:'序号',
            align:'center',
            formatter:function (value,row,index) {

                return [
                    '<p>'+(index+1)+'</p>',
                ].join('')}
        },
        {
            field:"jsName",
            title:"角色名称",
            align:"center"

        },
        {
            field:"dd",
            title:"操作",
            align:"center",
            formatter:function (value,row,index) {
                return [
                    "<a href='javascript:void(0);' onclick='toAdd("+row.id+",this)'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp;&nbsp;&nbsp;&nbsp;",
                    "<a href='javascript:void(0);' onclick='delRow("+row.id+")'><span class='glyphicon glyphicon-remove'></span></a>",
                ].join('')}

        }

            ]
})








// 角色权限修改保存
function  baocun(th,id) {

    //角色id   id
 var $checkArrays= $(th).parent().prev().find("input[type=checkbox]");
 var idArrays={};
 var k=0;
    $checkArrays.each(function () {
        if($(this).is(':checked')) {
            var qxid = $(this).next().attr("dataid")
            idArrays["list["+k+"]"]=qxid;
            k++;
        }
    })

   if($.isEmptyObject(idArrays)==false){
       idArrays["jid"]=id;
       swal({
           title: "您确定要保存您的修改信息吗",
           text: "保存后将退出登陆，请谨慎操作！",
           type: "warning",
           showCancelButton: true,
           cancelButtonText: "我再想想吧!",
           confirmButtonColor: "#DD6B55",
           confirmButtonText: "确定",
           closeOnConfirm: false
       }, function () {
           $.post("jueSe/baocun", idArrays, function (data) {

               if (data.result) {
                   swal({
                       title: "保存成功！",
                       text: "请您重新登陆！",
                       type: "success",
                   }, function () {
                       window.open("login/denglu","_parent");
                   })
               }
           }, 'json');
       })

   }else{
       swal("请至少选中一组权限","操作失败","error")
   }
}
//操作一级菜单提示
function  waring(th) {
    var check =$(th).is(':checked');
    if(check){
        //选中的时候
        var erMenuArrays=$(th).parents(".colorRed").siblings();
        erMenuArrays.each(function () {
            if($(this).find("input[type=checkbox]").is(':checked')){

            }else{
                $(th).get(0).checked = false;
                swal("当前菜单下应至少有一个权限被选中","操作失败","error")
                return;
            }

        })

    }else{
        //取消的时候
        //$(th).parents(".colorRed").siblings().find("input[type=checkbox]").is(':checked')
       var erMenuArrays=$(th).parents(".colorRed").siblings();
       erMenuArrays.each(function () {
        if($(this).find("input[type=checkbox]").is(':checked')){
            $(th).get(0).checked = true;
            swal("当前菜单下有权限分配","操作失败","error")
                return;
        }
       })

    }

}
//二级菜单选中操作
function erMenu(th) {
    var check =$(th).is(':checked');
  var $t=  $(th).parent().parent().find(".colorRed").find("input").get(0);
    if(check){
        //选中的时候

       $t.checked=true;

    }else{
        //取消的时候
        //$(th).parents(".colorRed").siblings().find("input[type=checkbox]").is(':checked')
        var erMenuArrays= $(th).parent().siblings(".erMenu");
        var s=false;
        erMenuArrays.each(function () {
            if($(this).find("input[type=checkbox]").is(':checked')) {

                s=true;
                return;
            }
        })
        $t.checked = s;

    }

}



//新增角色
function  addJueSe() {
    $('#modal').find('.modal-body').load('login/addJuese', function () {
        $('#modal').find('.modal-title').html("新增角色");
        $(".modal-footer").html(" <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">关闭</button>\n" +
            "            <button type=\"button\" class=\"btn btn-primary\" style='margin-top: 0px;outline: none' onclick='addJueseOk()' >确认</button>")
        $('#modal').modal();
    });
    
}
function  addJueseOk() {
 var jsName= $("#jsName").val();
    $.post("jueSe/AddJuese",{"jsName":jsName},function (data) {
        if(data.result>0){
            swal("成功添加","操作成功","success")
            $("#table").bootstrapTable("refresh")
            $('#modal').modal("hide");
        }
    });
}

//删除角色
function delRow(id) {
    swal({
        title: "您确定要删除信息吗",
        text: "删除后将不能恢复，请谨慎操作",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确定",
        closeOnConfirm: false
    }, function () {
        $.post("jueSe/deleteRole",{"id":id}, function (data) {
            if (data.result>0) {
                swal({
                    title: "删除成功！",
                    text: "操作成功！",
                    type: "success"
                })
                $("#table").bootstrapTable("refresh")
            }
        }, 'json');
    })
    
}


//更新角色
function  toAdd(id,th) {
    var jsName=$(th).parents("td").prev().text();
    $('#modal').find('.modal-body').load('login/addJuese',{"id":id,"jsName":jsName}, function () {
        $('#modal').find('.modal-title').html("更新角色姓名");
        $(".modal-footer").html(" <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">关闭</button>\n" +
            "            <button type=\"button\" class=\"btn btn-primary\" style='margin-top: 0px;outline: none' onclick='toAddOk()' >确认</button>")
        $('#modal').modal();
    });
}
function toAddOk(id) {

    var id=$("#id").val();
    console.log(id)
    var jsName= $("#jsName").val();
    $.post("jueSe/UpdateJuese",{"id":id,"jsName":jsName},function (data) {
        if(data.result>0){
            swal("更新信息完成","操作成功","success")
            $('#modal').modal("hide");
            $("#table").bootstrapTable("refresh")
        }
    });
}
